package com.example.mydemo.controller.hc;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mydemo.entity.Users;
import com.example.mydemo.entity.domain.MenuRoleVO;
import com.example.mydemo.entity.domain.ResponseDTO;
import com.example.mydemo.entity.domain.UserRoleVO;
import com.example.mydemo.entity.domain.dtree.DTree;
import com.example.mydemo.model.tableVO;
import com.example.mydemo.service.IMenuRoleService;
import com.example.mydemo.service.IUserRoleService;
import com.example.mydemo.service.UserService;
import com.example.mydemo.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author syf
 * @since 2025-05-28
 */
@RestController
@RequestMapping("/menu-role")
public class MenuRoleController {
    @Autowired
    private UserService userService;
    @Autowired
    private IMenuRoleService IMenuRoleService;

    @Autowired
    private RedisUtil redisUtil;
    @GetMapping("/getRoleMenupage")
    @ResponseBody
    public tableVO selectDeptCount(@RequestParam Long page, @RequestParam Long limit, @RequestParam(defaultValue = "") String searchParams) {
        Page<MenuRoleVO> deppage = new Page<>(page, limit); // 创建分页对象，当前页为1，每页显示5条记录
        List<MenuRoleVO> b = new ArrayList<>();
        if (searchParams == "") {
            b = IMenuRoleService.selectName();
        } else {
            b = IMenuRoleService.selectByRoleName(searchParams);
        }
        tableVO a = new tableVO();
        a.setCode(0L);
        a.setMsg("");
        a.setData(b);
        a.setCount((long) b.size());
        return a;
    }
    @PostMapping("/addRoleMenu")
    public ResponseDTO<MenuRoleVO> getUsers2(@RequestParam("menuList") List<DTree> menuId , @RequestParam("roleId") String roleId) {
        for (DTree n:menuId){
            MenuRoleVO userRoleVO = new MenuRoleVO();
            userRoleVO.setMenuId(Long.valueOf(n.getId()));
            userRoleVO.setRoleId(Long.valueOf(roleId));
            userRoleVO.setCreateBy("syf");
            userRoleVO.setCreateTime( LocalDateTime.now());
            IMenuRoleService.saveUser(userRoleVO);
        }

        return new ResponseDTO<MenuRoleVO>(200, "添加信息成功！", null);
    }
    @PostMapping("/RoleMenuDelete")
    @ResponseBody
    public ResponseDTO<MenuRoleVO> delete(@RequestParam Long id) {
        IMenuRoleService.delete(id);
        return new ResponseDTO<>(200, "删除信息成功！", null);
    }
}
